(function (doc, win) {
    var docEl = doc.documentElement,
        resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
        recalc = function () {
            var clientWidth = docEl.clientWidth;
            if (!clientWidth) return;
            docEl.style.fontSize = parseInt(20 * (clientWidth / 320)) + 'px';
        };
    if (!doc.addEventListener) return;
    win.addEventListener(resizeEvt, recalc, false);
    doc.addEventListener('DOMContentLoaded', recalc, false);
})(document, window);
$(function () {
    var LocalStorage = function () {
        var arr = [];
        var obj = $('#test');
        function render(arr) {
            var str = '';
            for (var i = 0; i < arr.length; i++) {
                str += ' <a href="javascript:;">' + arr[i] + '</a>';
            }
            return str;
        }
        function getValue(keycode, data) {
            if (keycode == 13) {
                var re = /\s*\s+/g;
                var b = re.test(data);
                if (data === '' || b === true) {
                    $('.mask').show();
                    $('#search').get(0).blur();
                    return;
                }
                set($.trim(data))
            }
        }
        function set(value) {
            $('.history-box').show();
            if (arr.length > 10) {
                arr.pop();
            }
            arr.unshift(value);
            var historys = JSON.stringify(arr);
            localStorage.setItem(history, historys);
            get();
        };
        function get() {
            var historys = JSON.parse(localStorage.getItem(history));
            if (historys == null)return;
            $('.history-box').show();
            var str = render(historys);
            obj.html(str);
        };
        function clear() {
            localStorage.removeItem(history);
            $('.history-box').hide();
            obj.html('');
            arr = [];
        };
        function init() {
            var aInit = JSON.parse(localStorage.getItem(history));
            if (aInit == null)return;
            arr = aInit;
        }
        init();
        return {
            getValue: getValue,
            set: set,
            get: get,
            clear: clear
        }
    }();
    LocalStorage.get();
    $('.history-clear').on('tap', function () {
        LocalStorage.clear()
    });
    $('#search').on('keyup', function (e) {
        LocalStorage.getValue(e.keyCode, $(this).val());
    })
    $('.search-cancel').on('tap', function () {
        $('.search').hide();
        $('.main').show();
    });
    $('.warning-confirm').on('click', function () {
        $('.mask').hide();
        $('#search').get(0).focus();
    })
    $('.label').on('tap', function () {
        $('.search').show();
        $('.main').hide();
        $('#search').get(0).focus();
    })
    $('.choose').on('tap', function () {
        LocalStorage.set($(this).text())
    })
})
$(function () {
    var oSlider = $('.slider-main');
    var iWidth = oSlider.width();
    var oPic = $('.slider-main ul:eq(0)');
    var oDot = $('.slider-main ul:eq(1)');
    var aDot = oDot.find('li');
    var index = 1;
    var timer = null;
    var startX = 0;
    var endX = 0;
    var moveX = 0;
    var distanceX = 0;
    var isMove = false;
    function init() {
        oPic.css({
            'transform': 'translate3d(' + (-index * iWidth) + 'px,0,0)'
        })
    }
    function move() {
        oPic.animate({
            'transform': 'translate3d(' + (-index * iWidth) + 'px,0,0)'
        }, 500, 'ease-out', function () {
            if (index >= 4) {
                index = 1;
                oPic.css({
                    'transform': 'translate3d(' + (-index * iWidth) + 'px,0,0)'
                })
            } else if (index <= 0) {
                index = 3;
                oPic.css({
                    'transform': 'translate3d(' + (-index * iWidth) + 'px,0,0)'
                })
            }
            aDot.removeClass('current').eq(index - 1).addClass('current');
        })
    }
    timer = setInterval(function () {
        index++;
        move();
    }, 4000);
    function swipe(callback) {
        oSlider.on('touchstart', function (e) {
            startX = e.touches[0].clientX;
            clearInterval(timer);
        });
        oSlider.on('touchmove', function (e) {
            moveX = e.touches[0].clientX;
            distanceX = moveX - startX;
            oPic.css({
                'transform': 'translate3d(' + (-index * iWidth + distanceX) + 'px,0,0)'
            });
            isMove = true;
        });
        oSlider.on('touchend', function (e) {
            if (Math.abs(distanceX) > iWidth / 3 && isMove) {
                if (distanceX > 0) {
                    index--;
                } else {
                    index++
                }
                move();
            } else {
                move();
            }
            isMove = false;
            startX = 0;
            endX = 0;
            moveX = 0;
            clearInterval(timer);
            callback && callback();
        })
    }
    init();
    swipe(function () {
        timer = setInterval(function () {
            index++;
            move();
        }, 5000);
    });
})
$(function(){
    var oBox=$('.word-container ul');
    var index=1;
    var timer=null;
    var iHeight=$('.word-container-item:eq(0)').height();
    function init(){
        oBox.css({
            'transform': 'translate3d(0,' + (-index * iHeight) + 'px,0)'
        })
    }
    function move(){
        oBox.animate({
            'transform':'translate3d(0,'+(-index*iHeight)+'px,0)'
        },'ease-out',500,function(){
            if(index>=3){
                index=0;
                oBox.css({
                    'transform':'translate3d(0,'+(-index*iHeight)+'px,0)'
                })
            }
        })
    }
    init();
    timer=setInterval(function(){
        index++;
        move();
    },1500)
})
$(function(){
    var oTop=$('.to-top');
    var i=0;
    var oTab=$('.tab-floor');
    var iHeight=$('.word-slider').offset().top;
    var phone=$('.phone');
        var toTop=function(){
        function top(value){
            $('html,body').scrollTop(value);
        }
       function throttle(method,context){
            clearTimeout(method.tId);
            method.tId=setTimeout(function(){
                method.call(context);
            },500);
        }
        function getScrollTop(){
            var iTop=$(window).scrollTop();
            if(iTop>iHeight){
                phone.show();
                oTop.show()
            }else{
                phone.hide();
                oTop.hide();
            }
        }
        function visible(){
            throttle(getScrollTop,window);
        }
        return {
            throttle:throttle,
            top:top,
            visible:visible
        }
    }()
    $(window).scroll(function(){
     toTop.visible();
    })
    oTop.on('tap',function(){
       toTop.top(0);
          phone.hide();
                oTop.hide();
    })
})
$(function(){
    $('.i-ellipsis').on('tap',function(){
      $(this).addClass('active').siblings().removeClass('active').parent().next().find('ul').eq($(this).index()).addClass('show').siblings().removeClass('show');
    });
    $('.sub-list li').on('tap',function(){
        $(this).addClass('active').siblings().removeClass('active');
    })
})
$(function(){
    var oBox=$('.list-block');
    var viewHeight=$(window).height();
    var oDiv=$('.showmore');
    function getRect(){
        var oDivRect=oDiv.get(0).getBoundingClientRect();
        if(oDivRect.bottom<viewHeight&&oDivRect.top>0){
            moduleLoad();
        }
        var pics=$('img[data-src]');
        $.each(pics,function(index,ele){
           var rect;
            if(ele.dataset.src=='')return;
            rect=ele.getBoundingClientRect();
           if(rect.bottom>=0&&rect.top<viewHeight){
               lazyLoad(ele);
           }
        })
    }
    function lazyLoad(obj){
        var img=new Image();
        img.src=obj.dataset.src;
        obj.dataset.src='';
        img.onload=function(){
        obj.src=img.src;
        obj.classList.add('fade');
        }
    }
    function moduleLoad(){
        var index=oDiv.get(0).dataset.id;
        if(oDiv.get(0).dataset.id>2){
            oDiv.html('已经是最后一项');
            return;
        }
        var data=[
            {
                path:'./img/banner/bkk.jpg',
                price:1800,
                promo:'已省&yen;400',
                people:'3万人出游',
                sati:'99%满意',
                title:'[五一]泰国-曼谷-芭提雅7日游',
                recommend:'',
                describe:'万人出游热销线，仅三站购物店，两千多好评，冬季躲霾初访微笑国度，经典必玩'
            },
            {   path:'./img/banner/hak.jpg',
                price:1999,
                promo:'已省&yen;180',
                people:'3.3万人出游',
                sati:'90%满意',
                title:'海南三亚-蜈支洲-天堂-天涯5日游',
                recommend:'',
                describe:'全年爆单，纯玩0购物，宿180海景不挪窝，24H专车接机，10万人出游'
            },
            { path:'./img/banner/cdg.jpg',
                price:15999,
                promo:'已省&yen;1000',
                people:'8861人出游',
                sati:'96%满意',
                title:'[端午]法瑞意13-14日游',
                recommend:'超人气<br/>推荐',
                describe:'林志颖同款，意大利深度，四五星，庄园酒店，五渔村，少女峰，比萨斜塔，金色快车，拒签无损，一价全含'}
        ]
        var str="<li class='tab-product-item'>";
        str+='<a href="javascript:;">';
        str+="<div class='item-top'>";
        str+="<div class='product-image'>";
        str+='<img src="./img/icon/loading2.png" data-src='+data[index].path+' alt="">';
        str+='<div class="image-gra"></div>';
        str+='</div>';
        str+="<div class='sati-peo'>";
        str+='<div class="peo">'+data[index].people+'</div>'
        str+='<div class="sati">'+data[index].sati+'</div>'
        str+='</div>';
        str+="<div class='product-extra'>";
        str+='<span class="icon icon-tours"></span>';
        str+='<span>跟团游</span>';
        str+='<span class="separator">|</span>';
        str+='<span>上海</span>';
        str+='</div>';
        str+="<div class='product-price price-schema-1'>";
        str+='<div class="product-price-now ">&yen;<em>'+data[index].price+'</em>起</div>';
        str+='<div class="product-promotion">'+data[index].promo+'</div>';
        str+='</div>';
        if(data[index].recommend!=''){
            str+='<div class="product-recommend">';
            str+='<span class="line">';
            str+=data[index].recommend;
            str+='</span>';
            str+='</div>';
        }
        str+='</div>';
        str+="<div class='item-bottom'>"
        str+='<h5 class="product-title">'+data[index].title+'</h5>';
        str+='<p class="product-describe">'+data[index].describe+'</p>';
        str+='<div class="product-tags"><span class="label hairlines" style="border-color:#FA7864;color:#FA7864;">立减</span><span class="label hairlines" style="border-color:#eeaa44;color:#eeaa44;">手机优惠价</span><span class="label hairlines" style="border-color:#cccccc;color:#999999;">必玩景点全含</span><span class="label hairlines" style="border-color:#cccccc;color:#999999;">首次参团必选</span></div>'
        str+='</div>';
        str+='</a>';
        str+='</li>';
        oDiv.html('加载中');
        oDiv.get(0).dataset.id++;
        setTimeout(function(){
        oDiv.html('加载更多');
        $('.list-block').append(str);
        },200)
    }
    function throttle(method,context){
        clearTimeout(method.tId);
        method.tId=setTimeout(function(){
            method.call(context);
        },300);
    }
    $(window).scroll(function(){
      throttle(getRect,window);
    })
})